﻿/*
 * @Description:用于广告管理
 */
var EstateSearch = {
    Autocomplete: function () {
        var txtEstateName = $("#txtEstateName");
        var hidEstateKeyId = $("#hidEstateKeyId");
        var txtBuildingName = $("#txtBuildingName");
        hidBuildingKeyId = $("#hidBuildingKeyId")
        var txtHouseNo = $("#txtHouseNo");
        hidHouseKeyId = $("#hidHouseKeyId");
        //楼盘自动完成及赋值隐藏域
        txtEstateName.change(function () {
            $(this).removeClass("checkError").next("span[class=wrong]").remove();
            hidEstateKeyId.val("");
            txtBuildingName.val("");
            hidBuildingKeyId.val("");
            txtHouseNo.val(""); 
            hidHouseKeyId.val("");
            txtBuildingName.removeAttr("readonly"); 

        });
        txtEstateName.blur(function () { });
        txtEstateName.autocomplete(function () {
            //此处对房间的搜索
            return "/property/estates-for-region-keyids-obtain-ajax?EstateName=" + escape(txtEstateName.val());
        }, {
            width: 260,
            max: 12,
            selectFirst: false,
            cacheLength: 0,
            formatItem: function (row) {
                return row.EstateFullName;
            },
            formatResult: function (row) {
                return row.EstateName;
            }
        }).result(function (a, b) { 
            hidEstateKeyId.val(b.KeyId); 
            txtBuildingName.removeAttr("readonly");
            txtEstateName.removeClass("checkError").next("span[class=wrong]").remove(); 
        }); 
        //栋座信息
        txtBuildingName.change(function () {
            hidBuildingKeyId.val(""); 
            //联动房间信息
            txtHouseNo.val("");
            hidHouseKeyId.val("");
            if (txtBuildingName.val()) {
                txtHouseNo.removeAttr("readonly").removeClass("checkError").next("span[class=wrong]").remove();
            } else {
                txtHouseNo.attr("readonly", "readonly");
            } 
        });

        txtBuildingName.focus(function () {
            if (txtEstateName.val()) {
                txtBuildingName.removeAttr("readonly").removeClass("checkError").next("span[class=wrong]").remove();
            } else {
                txtBuildingName.attr("readonly", "readonly");
            }
        });
        txtBuildingName.autocomplete(function () {
            if (txtEstateName.val()) {
                return "/property/building-for-estate-obatin-ajax?EstateName=" + escape(txtEstateName.val()) + '&BuildName=' + escape(txtBuildingName.val());
            }
            return '';
        }, {
            width: 260,
            max: 1000,
            cacheLength: 0,
            selectFirst: false,
            minChars: 0,
            clickMode: true,
            formatItem: function (row) {
                return row.BuildingName;
            },
            formatResult: function (row) {
                return row.BuildingName;
            }
        }).result(function (a, b) {
            hidBuildingKeyId.val(b.KeyId);
            // if (!b.IsBanAdd) {
            //hidBuildingKeyId.val(b.KeyId);
            txtHouseNo.removeAttr("readonly"); 
            txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove();
            //}

        });
        //盘源归属智能提示Start
        EmployeePrompt.getDepartAndEmployeeByInput('txtDepartmentName', 'hidDepartmentId', 'txtEmployeeName', 'hidUserId', false, 'private', 1, '业务员姓名', '请输入部门名称', true);
        //盘源归属智能提示End


        //房间信息
        txtHouseNo.focus(function () {
            if (txtBuildingName.val()) {
                txtHouseNo.removeAttr("readonly").removeClass("checkError").next("span[class=wrong]").remove();
            } else {
                txtHouseNo.attr("readonly", "readonly");
            }
            // txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove();
        });
        txtHouseNo.change(function () {
            hidHouseKeyId.val("");
            txtHouseNo.removeClass("checkError").next("span[class=wrong]").remove(); 
        });
        //自动完成房间填充
        txtHouseNo.autocomplete(function () {
            return "/estate/auto-match-house-no?BuildingKeyId=" + hidBuildingKeyId.val() + "&EstateKeyId=" + hidEstateKeyId.val() + "&HouseNo="+ txtHouseNo.val();
        }, {
            width: 260,
            max: 12,
            cacheLength: 0,
            selectFirst: false,
            minChars: 0,
            clickMode: true,
            formatItem: function (row) {
                return row.ItemText;
            },
            formatResult: function (row) {
                return row.ItemText;
            }
        }).result(function (a, b) {
            hidHouseKeyId.val(b.ItemValue); 
        });

        
    },
    BeForValid: function () { return true;},
    subFrom: function () {
        var txtEstateName = $("#txtEstateName");
        var hidEstateKeyId = $("#hidEstateKeyId");
        var txtBuildingName = $("#txtBuildingName");
        var hidBuildingKeyId = $("#hidBuildingKeyId")
        var txtHouseNo = $("#txtHouseNo");
        var hidHouseKeyId = $("#hidHouseKeyId");
        var txtDepartmentName = $("#txtDepartmentName");
        var hidDepartmentId = $("#hidDepartmentId");
        var txtEmployeeName = $("#txtEmployeeName");
        var hidUserId = $("#hidUserId");
        
        if (hidHouseKeyId.val() == "" || hidHouseKeyId.val() == undefined || !this.IsGuid(hidHouseKeyId.val())) {
            hidHouseKeyId.val("");
            txtHouseNo.val("");
            if (hidBuildingKeyId.val() == "" || hidBuildingKeyId.val() == undefined || !this.IsGuid(hidBuildingKeyId.val())) {
                hidBuildingKeyId.val("");
                txtBuildingName.val("");
                if (hidEstateKeyId.val() == "" || hidEstateKeyId.val() == undefined || !this.IsGuid(hidEstateKeyId.val())) { 
                    hidEstateKeyId.val("");
                    txtEstateName.val("");
                }
            }
        }

        if (hidUserId.val() == "" || hidUserId.val() == undefined || !this.IsGuid(hidUserId.val()))
        {
            hidUserId.val("");
            txtEmployeeName.val("业务员姓名");
            txtEmployeeName.focus().blur();
            if (hidDepartmentId.val() == "" || hidDepartmentId.val() == undefined || !this.IsGuid(hidDepartmentId.val()))
            {
                hidDepartmentId.val("");
                txtDepartmentName.val("请输入部门名称");
                txtDepartmentName.focus().blur();
            }
        }
        if (EstateSearch.BeForValid() == false)
            return false;
        $("#searchForm").submit();
    },
    IsGuid: function (str) {
        var reg0 = /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$|^[0-9a-f]{32}$/;
        var reg1 = /^[0-9a-f]{8}([0-9a-f]{4}){3}[0-9a-f]{12}$|^[0-9a-f]{32}$/;
        return reg0.test(str)||reg1.test(str);
    }
}; 

function setPageStartTime() {
    window.pageStartTime = new Date();
}
function setPageEndTime() {
    $("#timeCount").html("查询耗时" + timehelper.TimeDiffirence(window.pageStartTime, new Date()).msDiff + "毫秒");
}

var loadscript = {};

EstateSearch.beginSearch = function () {
    //此方法在分页控件中
    if ("undefined" != typeof ajaxpageronbegin) {
        ajaxpageronbegin();
    }
    setPageStartTime();
};
EstateSearch.pageOnSuccessFun = function () {
    setPageEndTime();
};

//新盘跟盘
$(":checkbox[name=NewProperty]").unbind("click");
$(":checkbox[name=NewProperty]").bind("click", function () { 
    var arr = $(":checkbox[name=NewProperty]");
    var i = 0;
    for (var j = 0; j < arr.length; j++) {
        if (arr[j].checked)
            i++;
    }
    if (i == 2) {
        $("#hidIsNewProperty").val("");
        $("#searchForm").submit();
    }
    else {
        if ($(this)[0].checked) {
            var clickfor = $(this).attr("clickfor");
            if (clickfor == "theNewSite") {
                $("#hidIsNewProperty").val("true");
            }
            else if (clickfor == "withTheDisk") {
                $("#hidIsNewProperty").val("false");
            }
        }
        else {
            if (arr[0].checked) {
                $("#hidIsNewProperty").val("true");
            }
            else if (arr[1].checked) {
                $("#hidIsNewProperty").val("false");
            }
            else {
                $("#hidIsNewProperty").val("");
            }
        }
        $("#searchForm").submit();
    }
});

//笋盘 非笋盘
$(":checkbox[name=HotProperty]").unbind("click");
$(":checkbox[name=HotProperty]").bind("click", function () {
    var arr = $(":checkbox[name=HotProperty]");
    var i = 0;
    for (var j = 0; j < arr.length; j++) {
        if (arr[j].checked)
            i++;
    }
    if (i == 2) {
        $("#hidIsHotProperty").val("");
        $("#searchForm").submit();
    }
    else {
        if ($(this)[0].checked) {
            var clickfor = $(this).attr("clickfor");
            if (clickfor == "isHotProperty") {
                $("#hidIsHotProperty").val("true");
            }
            else if (clickfor == "notHotProperty") {
                $("#hidIsHotProperty").val("false");
            }
        }
        else {
            if (arr[0].checked) {
                $("#hidIsHotProperty").val("true");
            }
            else if (arr[1].checked) {
                $("#hidIsHotProperty").val("false");
            }
            else {
                $("#hidIsHotProperty").val("");
            }
        }
        $("#searchForm").submit();
    }
});


$(function () {
    //浮动的工具栏 yjj添加
    var state1 = true; //存储顶部显示的状态
    $(window).scroll(function () {
        if ($(window).scrollTop() > $("#ajaxPages").offset().top) {
            if (state1) {
                //执行浮动层出现后的代码
                $(".batch").addClass("float-edit-toos");
                $(".fixed-div").show();
                if ($("#btnCloneHistory").attr("class") == "current") {
                    $(".fixed-div").css("top", "0");
                }
            }
            state1 = false;
        } else {
            if (!state1) {
                //执行浮动层出现前的代码
                $(".batch").removeClass("float-edit-toos");
                $(".fixed-div").hide();
            }
            state1 = true;
        }
    });
});